package org.gexperiments.graph;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.renderers.Renderer.Vertex;
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;

public class ImageRenderer implements Vertex<Node, Link> {

	private static Image image;

	static {
		image = new ImageIcon(ImageRenderer.class.getClassLoader().getResource("osa_hub.png")).getImage()
				.getScaledInstance(50, 50, Image.SCALE_SMOOTH);
	}

	public ImageRenderer() {
	}

	@Override
	public void paintVertex(RenderContext<Node, Link> rc, Layout<Node, Link> layout, Node vertex) {
		GraphicsDecorator g = rc.getGraphicsContext();
		Point2D center = layout.transform(vertex);
		g.drawImage(image, (int) center.getX(), (int) center.getY(), null);
		g.setFont(new Font("TimesRoman", Font.PLAIN, 10));
		g.setColor(Color.red);
		g.drawString(vertex.getUserLabel(), (int) center.getX(), (int) center.getY() + image.getHeight(null));
		g.setColor(Color.blue);
		g.drawString(vertex.getIp(), (int) center.getX(), (int) center.getY() + image.getHeight(null)
				+ g.getFont().getSize() + 1);

	}
}